package ch.wachsmuth.gtugzh.contacts.impl;

import ch.wachsmuth.gtugzh.contacts.api.Address;
import ch.wachsmuth.gtugzh.contacts.api.Contacts;
import ch.wachsmuth.gtugzh.contacts.api.GoogleContacts;

import com.google.gdata.client.Query;
import com.google.gdata.client.contacts.*;
import com.google.gdata.data.*;
import com.google.gdata.data.contacts.ContactEntry;
import com.google.gdata.data.contacts.ContactFeed;
import com.google.gdata.data.extensions.*;
import com.google.gdata.util.*;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class GoogleContactsImpl implements GoogleContacts {
	private ContactsService myService = null;
	private URL feedUrl = null;
	private Query myQuery = null;
	private String eMail;
	private String userName;
	private String password;
	private int maxContactResults;

	public GoogleContactsImpl(String eMail, String userName, String password, int maxContactResults)
			throws MalformedURLException, AuthenticationException {
		this.eMail = eMail;
		this.userName = userName;
		this.password = password;
		this.maxContactResults = maxContactResults;

		createService();
	}

	private void createService() throws MalformedURLException,
			AuthenticationException {
		feedUrl = new URL("https://www.google.com/m8/feeds/contacts/" + eMail
				+ "/full");
		myService = new ContactsService("Google Contacts Searcher");
		myService.setUserCredentials(this.userName, this.password);
		myQuery = new Query(feedUrl);
		myQuery.setMaxResults(this.maxContactResults);
	}

	public List<Contacts> getContactList() throws ServiceException,
			IOException {
		List<Contacts> contacts = new ArrayList<Contacts>();

		ContactFeed resultFeed = myService.getFeed(myQuery, ContactFeed.class);

		for (ContactEntry entry : resultFeed.getEntries()) {
			ContactsImpl contact = new ContactsImpl();

			if (entry.hasName()) {
				Name name = entry.getName();
				if (name.hasFullName()) {
					String fullNameToDisplay = name.getFullName().getValue();
					if (name.getFullName().hasYomi()) {
						fullNameToDisplay += " ("
								+ name.getFullName().getYomi() + ")";
					}
					String[] fullName = fullNameToDisplay.split(" ");

					if (fullName.length > 1) {
						contact.setGivenName(fullName[0]);
						contact.setFamilyName(fullName[1]);
					}
				} else {
					System.out.println("\t\t (no full name found)");
				}
				if (name.hasGivenName()) {
					String givenNameToDisplay = name.getGivenName().getValue();
					if (name.getGivenName().hasYomi()) {
						givenNameToDisplay += " ("
								+ name.getGivenName().getYomi() + ")";
					}
					contact.setGivenName(givenNameToDisplay);
				} else {
					System.out.println("\t\t (no given name found)");
				}
				if (name.hasFamilyName()) {
					String familyNameToDisplay = name.getFamilyName()
							.getValue();
					if (name.getFamilyName().hasYomi()) {
						familyNameToDisplay += " ("
								+ name.getFamilyName().getYomi() + ")";
					}
					contact.setFamilyName(familyNameToDisplay);
				} else {
					System.out.println("\t\t (no family name found)");
				}
			} else {
				System.out.println("\t (no name found)");
			}

			if (entry.hasStructuredPostalAddresses()) {
				Address adr = new AddressImpl();
				for (StructuredPostalAddress paddr : entry
						.getStructuredPostalAddresses()) {
					if (paddr.hasPrimary()) {
						adr.setStreet(paddr.getStreet().getValue());
						adr.setZipCode(paddr.getPostcode().getValue());
						adr.setCity(paddr.getCity().getValue());
						adr.setCountry(paddr.getCountry().getValue());
						contact.setAddressObj(adr);
						break;
					}
				}

			}
			
			Link photoLink = entry.getContactPhotoLink();
			String photoLinkHref = photoLink.getHref();
			contact.setPictureURL(photoLinkHref);

			contacts.add(contact);
		}

		return contacts;
	}
}
